package com.biz.primus.ms.commodity.service.backend;

import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.base.enums.vendor.VendorType;
import com.biz.primus.common.utils.ValueUtils;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.commodity.dao.po.Brand;
import com.biz.primus.ms.commodity.dao.po.Category;
import com.biz.primus.ms.commodity.dao.po.Extend;
import com.biz.primus.ms.commodity.dao.po.Product;
import com.biz.primus.ms.commodity.dao.redis.BrandRedisDao;
import com.biz.primus.ms.commodity.dao.redis.CategoryRedisDao;
import com.biz.primus.ms.commodity.dao.redis.ProductRedisDao;
import com.biz.primus.ms.commodity.dao.redis.extend.ExtendRedisDao;
import com.biz.primus.ms.commodity.dao.repository.brand.BrandRepository;
import com.biz.primus.ms.commodity.dao.repository.category.CategoryRepository;
import com.biz.primus.ms.commodity.dao.repository.product.ExtendRepository;
import com.biz.primus.ms.commodity.dao.repository.product.ProductRepository;
import com.biz.primus.ms.commodity.dao.ro.BrandRo;
import com.biz.primus.ms.commodity.dao.ro.CategoryRo;
import com.biz.primus.ms.commodity.dao.ro.ProductRo;
import com.biz.primus.ms.commodity.trans.Brand2BrandRo;
import com.biz.primus.ms.commodity.trans.Category2CategoryRo;
import com.biz.primus.ms.commodity.trans.Extend2ExtendRo;
import com.biz.primus.ms.commodity.trans.Product2ProductRo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 江南
 * @usage
 * @reviewer
 * @since 2017/3/19
 */
@Slf4j
@Service
public class InitDataService extends AbstractBaseService {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private ProductRedisDao productRedisDao;

    @Autowired
    private BrandRepository brandRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private CategoryRedisDao categoryRedisDao;

    @Autowired
    private BrandRedisDao brandRedisDao;

    @Autowired
    private ExtendRedisDao extendRedisDao;

    @Autowired
    private ExtendRepository extendRepository;


    /**
     * 初始化ProductRo
     * 1.ProductRo
     *
     * @see ProductRedisDao#getTypeBProductsSortedSetKey()
     */
    public void initProductRo() {
        List<Product> typeBProduct = productRepository.findByProductType(VendorType.TYPE_B);
        for (Product product : typeBProduct) {
            ProductRo productRo = new Product2ProductRo(brandRepository, categoryRepository).apply(product);
            productRedisDao.save(productRo);
        }
    }

    /**
     * 初始化CategoryRo
     * 1.categoryRo
     *
     * @see CategoryRedisDao#appCategorySortSetKey()
     */
    public void initCategoryRo() {
        List<Category> categories = categoryRepository.findAll();
        for (Category category : categories) {
            CategoryRo categoryRo = new Category2CategoryRo().apply(category);
            categoryRedisDao.save(categoryRo);
        }
    }

    /**
     * 初始化brandRo
     * 1.brandRo
     *
     * @see BrandRedisDao#getAppBrandSortSetKey(java.lang.Long)
     */
    public void initBrandRo() {
        List<Brand> brands = brandRepository.findAll();
        for (Brand brand : brands) {
            BrandRo brandRo = new Brand2BrandRo().apply(brand);
            brandRedisDao.save(brandRo);
            List<Long> categoryIds = brand.getCategories().stream().map(Category::getId).collect(Collectors.toList());
            if (brand.getSaleChannel() == SaleChannel.ALL || brand.getSaleChannel() == SaleChannel.APP) {
                this.delayer.executeAfterTransactionCommit(() -> brandRedisDao.setAppBrandSort(brand.getId(), categoryIds,
                        Double.valueOf(ValueUtils.getValue(brand.getId()))));
            } else if (brand.getSaleChannel() == null || brand.getSaleChannel() == SaleChannel.WEB) {
                this.delayer.executeAfterTransactionCommit(() -> brandRedisDao.removeAppSort(brand.getId(), categoryIds));
            }
        }
    }

    /**
     * 初始化extendRo
     * 1.extendRo
     */
    public void initExtendRo() {
        List<Extend> extendList = extendRepository.findAll();
        extendRedisDao.save(extendList.stream().map(e->new Extend2ExtendRo().apply(e)).collect(Collectors.toList()));
    }

}
